package com.elink.enginerring.dict.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.elink.enginerring.common.ErrorType;
import com.elink.enginerring.common.Pagination;
import com.elink.enginerring.dict.co.DictCo;
import com.elink.enginerring.dict.dao.DictMapper;
import com.elink.enginerring.dict.entity.Dict;
import com.elink.enginerring.dict.entity.Word;
import com.elink.enginerring.util.JSONUtil;

@Service
public class DictService implements IDictService {
	@Autowired
	private DictMapper dictMapper;

	@Override
	public Map<String, Object> getListOfPage(DictCo dictCo) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", dictMapper.select(dictCo));
		map.put("pagination",new Pagination(dictCo, dictMapper.selectCount(dictCo)));
		return JSONUtil.getJSON(ErrorType.SUCCESS, map);
	}

	@Override
	public Dict getSingleDict(String byName) {
		return dictMapper.selectSingleDict(byName);
	}

	@Override
	public boolean deleteByDictId(int dictId) {
		return dictMapper.deleteByDictId(dictId) == 1 ? true : false;
	}

	@Override
	public boolean addOrEditDict(DictCo dictCo) {
		if (dictCo.getByName() == null) {
			return false;
		}
		Dict dict = new Dict();
		dict.setDictId(dictCo.getDictId());
		dict.setByName(dictCo.getByName());
		dict.setToName(dictCo.getToName());
		dict.setTranslate(dictCo.getTranslate());
		Dict dtmp = dictMapper.selectSingleDict(dictCo.getByName());
		System.out.println(dictCo.getIsCover());
		System.out.println(dictCo.getIsCover() == 1);
		// 不修改
		if (dictCo.getIsCover() != 1) {
			if (dtmp != null && dtmp.getByName().equals(dictCo.getByName())) {
				return false;
			}
			System.out.println("insert");
			if (Word.isChinese(dictCo.getByName())) {
				dict.setDictType(2);
			} else {
				dict.setDictType(1);
			}
			dictMapper.insert(dict);
		}else {// 判断是否存在 已存在则无法新增
			System.out.println("update");
			dictMapper.update(dict);
		}
		return true;
	}

	@Override
	public Map<String, Object> addDict(Dict dict,String merge) {
			if (Word.isChinese(dict.getByName())) {
				dict.setDictType(2);
			} else {
				dict.setDictType(1);
			}
			 dict.setCreateTime(new Date());
			if(merge !=""){
				//先根据此条删掉原来所有的
				 dictMapper.delOfByName(dict.getByName());
			}
			 dictMapper.insert(dict);
			 return JSONUtil.getJSON(ErrorType.SUCCESS);
	}

	@Override
	public void editDict(Dict dict) {
		Dict oldDict = 	dictMapper.selectByPrimaryKey(dict.getDictId());
		if(oldDict != null){
			dictMapper.update(dict);
		}
	}

	@Override
	public void delDict(String dictIds) {
		dictMapper.delDict(dictIds);
	}

	@Override
	public Map<String, Object> getInfo(int dictId) {
		return	JSONUtil.getJSON(ErrorType.SUCCESS,dictMapper.selectByPrimaryKey(dictId)); 
	}

	@Override
	public Map<String, Object> getDistinct(String byName) {
		List<Dict> dictList= 	dictMapper.selectDictByName(byName);
		if( dictList.size()  != 0){//该词条存在
			 return JSONUtil.getJSON(ErrorType.DICT_ERROR,dictList);
		}else{
			 return JSONUtil.getJSON(ErrorType.SUCCESS);
		}
	}
}
